package com.kehao.leetcode.jzoffer;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/9 13:39
 */
public class JZSolution011 {

    public int findMaxLength(int[] nums) {
        int length = 0;
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,-1);
        int sum =0;

        for (int i = 0; i < nums.length; i++) {
            sum += nums[i]==0?-1:1;
            if(map.containsKey(sum)){
                length = Integer.max(length,i-map.get(sum));
            }else {
                map.put(sum,i);
            }
        }
        return length;
    }

    @Test
    public void test01() {
        int nums[] = {0,0,1,0,0,0,1,1};
        int result = findMaxLength(nums);
        System.out.println(result);
    }
}
